//
//  HZBasisView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/9.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit
import SnapKit

class HZBasisView: UIView {
    
    //01 声明一个闭包
    var bb :(()->())?
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //监听按钮点击事件
    @objc private func BtnClick (){
        //03 执行闭包
        bb?()
        
        
    }
    
    
    
    
    //供外界调用的方法
    func setupBasis(imageName: String?, title: String?) {
        //代表设置的是消息，发现，我
        if let img = imageName, let tit = title {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            //隐藏圆
            RoundImageView.hidden = true
            
        }else {
            //代表首页
            setupAnim()
        }
    }
    
    
    //设置圆 动画
    func setupAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置
        anim.toValue = M_PI * 2
        //设置动画时长
        anim.duration = 10
        //设置循环次数
        anim.repeatCount = MAXFLOAT
        //如果用户切换自控制器或者退到后台 系统默认会移除动画  所以我们要把移除设置为否
        anim.removedOnCompletion = false
        //添加动画
        RoundImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        //01 添加控件
        addSubview(RoundImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(RegisterBtn)
        addSubview(loginBtn)
        
        //  设置约束
        //01
        RoundImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //02
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //03
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //04
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
        }
        //05
        RegisterBtn.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        //06
        loginBtn.snp_makeConstraints { (make) in
            make.size.top.equalTo(RegisterBtn)
            make.trailing.equalTo(messageLabel)
        }
    }
    
    //visitordiscover_feed_image_smallicon
    //懒加载控件
    //圆圈
    private lazy var RoundImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    //遮挡
    private lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //房子 visitordiscover_feed_image_house
    private lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //我
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = AppTheneColor
        lab.font = UIFont.systemFontOfSize(15)
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        lab.text = "关注一些人，回这里看看有什么惊喜"
        //设置最大显示宽度
        lab.preferredMaxLayoutWidth = 240
        return lab
    }()
    
    
    
    
    
    
    private lazy var RegisterBtn: UIButton = {
        let button = UIButton()
        //点击事件
        button.addTarget(self, action: #selector(HZBasisView.BtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(AppTheneColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return button
    }()
    
    private lazy var loginBtn: UIButton = {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "BtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(AppTheneColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return button
    }()
    
    

}
